/**
 * designed to improve on popup menu behavior.
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.util;

import java.awt.Window;

import javax.swing.JPopupMenu;

public class AlicePopupMenu extends JPopupMenu {

 protected int millisecondDelay = 400;

 protected javax.swing.Timer setPopupVisibleTrueTimer;
 protected javax.swing.Timer setPopupVisibleFalseTimer;
 AliceMenu invokerMenu;

 public AlicePopupMenu() {
  setLayout( new MultiColumnPopupLayout() );
 }

 // HACK for gray boxes bug; this can go away in 1.4
 //Rem @Override
 public void setVisible( boolean b ) {
  super.setVisible( b );
  java.awt.Container popup = null;
  if( b ) {
   popup = getParent();
   if( popup != null ) { // content pane
    popup = popup.getParent();
   }
   if( popup != null ) { // layered pane
    popup = popup.getParent();
   }
   if( popup != null ) { // root pane
    popup = popup.getParent();
   }
   if( popup instanceof java.awt.Window ) {
    java.awt.Window[] windows = ((java.awt.Window)popup).getOwnedWindows();
    //LOB for (Window window : windows)
    int t_tam = windows!=null ? windows.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     windows[i_i].setVisible(false); //L window.setVisible(false);
    }
   }
  }
 }

 //Rem @Override
 public void menuSelectionChanged( boolean isIncluded ) {
  if( setPopupVisibleTrueTimer == null ) {
   setPopupVisibleTrueTimer = new javax.swing.Timer( millisecondDelay, new java.awt.event.ActionListener() {
    public void actionPerformed( java.awt.event.ActionEvent ev ) {
     AlicePopupMenu.this.invokerMenu.setPopupMenuVisible( true );
    }
   } );
   setPopupVisibleTrueTimer.setRepeats( false );
  }
  if( setPopupVisibleFalseTimer == null ) {
   setPopupVisibleFalseTimer = new javax.swing.Timer( millisecondDelay, new java.awt.event.ActionListener() {
    public void actionPerformed( java.awt.event.ActionEvent ev ) {
     AlicePopupMenu.this.invokerMenu.setPopupMenuVisible( false );
    }
   } );
   setPopupVisibleFalseTimer.setRepeats( false );
  }
  if( setPopupVisibleTrueTimer.isRunning() ) {
   setPopupVisibleTrueTimer.stop();
  }
  if( setPopupVisibleFalseTimer.isRunning() ) {
   setPopupVisibleFalseTimer.stop();
  }

  if( getInvoker() instanceof AliceMenu ) {
   boolean allGone = javax.swing.MenuSelectionManager.defaultManager().getSelectedPath().length == 0;
   invokerMenu = (AliceMenu)getInvoker();

   if( isIncluded ) {
    setPopupVisibleTrueTimer.start();
   } else {
    if( allGone ) {
     setVisible( false );
    } else {
     setPopupVisibleFalseTimer.start();
    }
   }
  }
  if( isPopupMenu() && ! isIncluded ) {
   setVisible( false );
  }
 }

 private boolean isPopupMenu() {
  return ( (getInvoker() != null) && ! (getInvoker() instanceof AliceMenu) );
 }

 //Rem @Override
 public void show( java.awt.Component invoker, int x, int y ) {
  super.show( invoker, x, y );
  PopupMenuUtilities.ensurePopupIsOnScreen( this );
 }

}
